import axios from 'axios';

// 创建axios实例
const api = axios.create({
  baseURL: '',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 天气数据接口定义
export interface WeatherCurrent {
  city: string;
  adcode?: string;
  province?: string;
  reporttime?: string;
  weather: string;
  temperature: string;
  winddirection: string;
  windpower: string;
  humidity: string;
  temperature_float?: string;
  humidity_float?: string;
}

export interface WeatherForecast {
  date: string;
  week: string;
  dayweather: string;
  nightweather: string;
  daytemp: string;
  nighttemp: string;
  daywind: string;
  nightwind: string;
  daypower: string;
  nightpower: string;
  daytemp_float?: string;
  nighttemp_float?: string;
}

export interface WeatherData {
  city: string;
  current: WeatherCurrent | null;
  forecast: WeatherForecast[];
  updateTime: string;
  formattedText?: string;
}

export interface WeatherResponse {
  success: boolean;
  data: WeatherData;
  message: string;
}

/**
 * 获取指定城市的天气信息
 * @param city 城市名称
 * @returns Promise<WeatherResponse>
 */
export const getWeatherInfo = async (city: string): Promise<WeatherResponse> => {
  try {
    const response = await api.get(`/weather/${encodeURIComponent(city)}`);
    return response.data;
  } catch (error: any) {
    console.error('获取天气信息失败:', error);

    // 返回错误响应
    return {
      success: false,
      data: {
        city: city,
        current: null,
        forecast: [],
        updateTime: new Date().toISOString()
      },
      message: error.response?.data?.message || error.message || '获取天气信息失败'
    };
  }
};

/**
 * 获取天气图标
 * @param weather 天气状况
 * @returns 图标文件名
 */
export const getWeatherIcon = (weather: string): string => {
  // 根据天气状况返回对应的图标文件名
  if (weather.includes('晴')) {
    return 'sun.png';
  } else if (weather.includes('多云')) {
    return 'cloudy.png';
  } else if (weather.includes('阴')) {
    return '阴天.png';
  } else if (weather.includes('大暴雨')) {
    return '大暴雨.png';
  } else if (weather.includes('暴雨')) {
    return '大雨.png';
  } else if (weather.includes('大雨')) {
    return '大雨.png';
  } else if (weather.includes('中雨')) {
    return '中雨 .png';
  } else if (weather.includes('小雨')) {
    return '小雨.png';
  } else if (weather.includes('雷阵雨') || weather.includes('雷雨')) {
    return '雷阵雨.png';
  } else if (weather.includes('雨')) {
    return '小雨.png';
  } else if (weather.includes('雪')) {
    return '雪天.png';
  } else if (weather.includes('冰雹')) {
    return '冰雹.png';
  } else if (weather.includes('雾') || weather.includes('霾')) {
    return '雾.png';
  } else {
    return 'cloudy.png'; // 默认图标
  }
};

/**
 * 格式化日期显示
 * @param dateStr 日期字符串 (YYYY-MM-DD)
 * @returns 格式化后的日期 (MM/DD)
 */
export const formatDate = (dateStr: string): string => {
  try {
    const date = new Date(dateStr);
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${month}/${day}`;
  } catch (error) {
    return dateStr;
  }
};

/**
 * 获取星期显示
 * @param week 星期数字字符串
 * @returns 星期显示文本
 */
export const getWeekText = (week: string): string => {
  const weekMap: { [key: string]: string } = {
    '1': '周一',
    '2': '周二',
    '3': '周三',
    '4': '周四',
    '5': '周五',
    '6': '周六',
    '7': '周日'
  };
  return weekMap[week] || `周${week}`;
};

export default api;